cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        target:cc.Node,
        sprite:cc.SpriteFrame,
        checkedSprite:cc.SpriteFrame,
        checked:false,
        groupId:-1,
        secondtarget:cc.Node,
        secondsprite:cc.SpriteFrame,
        secondcheckedSprite:cc.SpriteFrame,
        targetLabel:cc.Node,
        targetColor:cc.Color,
        targetCheckColor:cc.Color,
        showChangeTarget:cc.Node
    },

    // use this for initialization
    onLoad: function () {
        if(cc.vv == null){
            return
        };
        if(cc.vv.radiogroupmgr == null){
            var RadioGroupMgr = require("RadioGroupMgr");
            cc.vv.radiogroupmgr = new RadioGroupMgr();
            cc.vv.radiogroupmgr.init()
        };
        // console.log(typeof(cc.vv.radiogroupmgr.add));
        cc.vv.radiogroupmgr.add(this);

        this.refresh()
    },
    
    refresh:function(){
        var targetSprite = this.target.getComponent(cc.Sprite);
        if (this.secondtarget) {
            var secondtargetSprite = this.secondtarget.getComponent(cc.Sprite)
        };
        if(this.checked){
            if (this.checkedSprite) {
                targetSprite.spriteFrame = this.checkedSprite
            };     
            if (secondtargetSprite) {    
                secondtargetSprite.spriteFrame = this.secondcheckedSprite
            }
        }else{
            targetSprite.spriteFrame = this.sprite
            if (secondtargetSprite) {
                 secondtargetSprite.spriteFrame = this.secondsprite
            }
        };
        if (this.targetLabel) {
            if (this.checked) {
                this.targetLabel.color=this.targetCheckColor
            }else{
                this.targetLabel.color=this.targetColor
            }
        };
        if (this.showChangeTarget) {
            if (this.checked) {
                this.showChangeTarget.active=true
            }else{
                this.showChangeTarget.active=false
            }
        }
    },
    
    check:function(value){
        this.checked = value;
        this.refresh()
    },
    
    onClicked:function(){
        console.log("RadioOnClicked");
        cc.vv.radiogroupmgr.check(this)
    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
    
    onDestroy:function(){
        if(cc.vv && cc.vv.radiogroupmgr){
            cc.vv.radiogroupmgr.del(this)   
        }
    }
});
